home *** CD-ROM | disk | FTP | other *** search
- /*
- File: TMP_RS_Config.c
-
- Contains: The residend portion of the a configurator
-
- This code is not intended to implement an actual
- working configurator. It comes with no warranty,
- express or implied, as to it's suitability for your
- own configuration implementation.
- */
-
- #ifndef __TMP_CONFIG__
- #include "TMP_Config.h"
- #endif
- #ifndef __OTSHAREDLIBS__
- #include "OTSharedLibs.h"
- #endif
-
- #if TMPCONFIG_USES_ASLM
- #include <LibraryManager.h>
- #endif
-
- /*******************************************************************************
- ** Forward declarations
- ********************************************************************************/
-
- static void ScheduleUnload();
- static pascal void DoUnload(void*);
-
- OSStatus OTSetupConfigurator(OTCanConfigureProcPtr* ccProc,
- OTCreateConfiguratorProcPtr* ccProc2,
- UInt8* typePtr);
-
- /*******************************************************************************
- ** Some Globals
- ********************************************************************************/
-
- #if TMPCONFIG_USES_CFM
- static UInt32 gMyCFMID;
- #endif
-
- /*
- * Flag that an unload is scheduled
- */
- Boolean gUnloadScheduled = false;
- /*
- * This is data shared between us and our "controller".
- */
- MySharedStruct gShared = { 0 };
- /*
- * List of protocols
- */
- static char* gList[] = { kProtocolList, 0 };
-
- /*******************************************************************************
- ** ScheduleUnload and DoUnload
- ********************************************************************************/
-
- static void ScheduleUnload()
- {
- gUnloadScheduled = true;
- OTScheduleSystemTask(gShared.systemTask);
- }
-
- static pascal void DoUnload(void* dummy)
- {
- #pragma unused(dummy)
-
- if ( gUnloadScheduled )
- {
- gUnloadScheduled = false;
- /*
- * Note that we destroy the system task, but we let the controller
- * library create it. That's because we aren't a client of Open Transport
- * (i.e. we didn't call InitOpenTransport), so we can't do anything that
- * will allocate memory or create endpoints or we'll crash.
- * We can, however, free memory, since that doesn't require
- * Open Transport to know about us (as long as Open Transport is really
- * loaded, which we know to be true, because our main code DID do an
- * InitOpenTransport, and will not do a CloseOpenTransport until we
- * unload them in the next couple of lines).
- */
- OTDestroySystemTask(gShared.systemTask);
- gShared.systemTask = 0;
-
- #if TMPCONFIG_USES_CFM
- OTReleaseCFMConnection(&gMyCFMID);
- #else
- OTUnloadASLMLibrary(kTMPConfigASLMID);
- /*
- * This is an ASLM function to force the libraries to unload "right now".
- * Otherwise, they will not unload until the next system task.
- */
- UnloadUnusedLibraries();
- #endif
- }
- }
-
- /*******************************************************************************
- ** TMP_CanConfigure
- **
- ** This function determines if we can configure the supplied configuration.
- ** It normally looks at the top name in the configuration to see if it's one of
- ** the protocols or drivers that we handle.
- ********************************************************************************/
-
- static Boolean TMP_CanConfigure(OTConfiguration* cfig, UInt32 pass)
- {
- #pragma unused(pass)
-
- const char* name = OTCfigGetProviderName(cfig);
- SInt16 index = -1;
- while ( gList[++index] != NULL )
- {
- if ( OTStrEqual(name, gList[index]) )
- return true;
- }
- return false;
- }
-
- /*******************************************************************************
- ** TMP_CreateConfigurator
- **
- ** This function loads and creates our configurator. It is called by the
- ** system when someone wants the configurator, and we're not already loaded.
- ********************************************************************************/
-
- static OSStatus TMP_CreateConfigurator(TOTConfigurator** cfigPtr)
- {
- ProcPtr proc;
- OSStatus err;
- #if TMPCONFIG_USES_ASLM
- OSErr osErr;
- #endif
- /*
- * If we're scheduled to unload, do it so we get a fresh copy
- */
- if ( gUnloadScheduled )
- {
- DoUnload(NULL);
- }
- /*
- * Set up the vectors that our configurator code needs to request itself
- * to be unloaded. Unfortunately, the configurator cannot unload itself
- * or a crash might occur, since under certain conditions using CFM and
- * VM on, the code memory becomes unusable immediately upon calling
- * CloseConnection.
- */
- gShared.scheduleUnloadProc = &ScheduleUnload;
- gShared.unloadProc = &DoUnload;
- /*
- * Here, we load in our shared library and invoke it's 'OTCreateMyConfigurator'
- * function that we exported.
- */
- #if TMPCONFIG_USES_CFM
-
- proc = (ProcPtr)OTGetCFMPointer(kTMPConfigCFMMID, "OTCreateMyConfigurator", &gMyCFMID,
- kOTLoadACopy | kOTGetCodeSymbol);
- if ( proc == 0 )
- return kENXIOErr;
-
- err = (*(CreateMyConfigProcPtr)proc)(&gShared);
-
- #else
-
- osErr = OTLoadASLMLibrary(kTMPConfigASLMID);
- if ( osErr != 0 )
- return osErr;
-
- proc = (ProcPtr)GetFunctionPointer(kTMPConfigASLMID,
- "OTCreateMyConfigurator", &osErr);
- if ( proc == 0 )
- {
- err = osErr;
- }
- else
- {
- err = (*(CreateMyConfigProcPtr)proc)(&gShared);
- }
-
- #endif
- /*
- * If an error occurred, unload the configurator code. Otherwise, copy
- * the configurator pointer to the client.
- */
- if ( err != kOTNoError )
- {
- gUnloadScheduled = true; /* Lie, so DoUnload will cause the unload */
- DoUnload(NULL);
- }
- else
- {
- *cfigPtr = gShared.cfig; /* Save the pointer to our controller */
- }
- return err;
- }
-
- /*******************************************************************************
- ** OTSetupConfigurator
- **
- ** The system calls this function when the system first comes up to get our
- ** entry points for configuring.
- ********************************************************************************/
-
- OSStatus OTSetupConfigurator(OTCanConfigureProcPtr* ccProc,
- OTCreateConfiguratorProcPtr* ccProc2,
- UInt8* typePtr)
- {
- /*
- * Select the typePtr that's appropriate.
- */
- *typePtr = kOTProtocolFamilyConfigurator;
- *typePtr = kOTLinkDriverConfigurator;
-
- *ccProc = &TMP_CanConfigure;
- *ccProc2 = &TMP_CreateConfigurator;
- return kOTNoError;
- }
-